package com.ssy.wireless.web.controller;

import com.github.pagehelper.Page;
import com.ssy.wireless.entity.Account;
import com.ssy.wireless.util.DateUtils;
import io.swagger.models.auth.In;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by cofey on 16/11/20.
 */
abstract class AbstractController {

    protected Account getAccount() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        Object currUser = request.getSession().getAttribute("currUser");
        return null != currUser ? (Account) currUser : null;
    }

    protected Map<String, Object> result(Page page) {
        Map<String, Object> result = new HashMap();
        result.put("items", page.getResult());
        result.put("pageIndex", page.getStartRow());
        result.put("pageSize", page.getPageSize());
        result.put("total", page.getTotal());
        return result;
    }

    protected class xAxisAndBeginAndEndDate {
        private int begin;
        private int end;
        private List<String> xAxis;

        public int getBegin() {
            return begin;
        }

        public void setBegin(int begin) {
            this.begin = begin;
        }

        public int getEnd() {
            return end;
        }

        public void setEnd(int end) {
            this.end = end;
        }

        public List<String> getxAxis() {
            return xAxis;
        }

        public void setxAxis(List<String> xAxis) {
            this.xAxis = xAxis;
        }
    }

    protected xAxisAndBeginAndEndDate setxAxisAndBeginAndEndDate(int type, String beginDate, String endDate) {
        xAxisAndBeginAndEndDate vo = new xAxisAndBeginAndEndDate();
        int begin = 0, end = 0;
        List<String> xAxis = new ArrayList<>();
        switch (type) {
            case 1:
                begin = Integer.valueOf(DateUtils.curDate().replaceAll("-", "") + "00");
                end = Integer.valueOf(DateUtils.curDate().replaceAll("-", "") + "23");
                for (int i = begin; i <= end; i++) {
                    String dateHour = i + "";
                    dateHour = dateHour.substring(dateHour.length() - 2, dateHour.length());
                    xAxis.add(dateHour);
                }
                begin = Integer.valueOf(DateUtils.format(new Date(), "yyyyMMdd"));
                break;
            case 2:
                int days = DateUtils.daysBetween(DateUtils.parseYMD(beginDate), DateUtils.parseYMD(endDate));
                begin = Integer.valueOf(beginDate.replaceAll("-", ""));
                end = Integer.valueOf(endDate.replaceAll("-", ""));
                for (int i = 0; i <= days; i++) {
                    xAxis.add(DateUtils.formatYMD(DateUtils.dayAdd(DateUtils.parseYMD(beginDate), i)));
                }
                break;
            case 3:

                break;
            case 4:
                int months = DateUtils.monthsBetween(DateUtils.parse(beginDate, "yyyy-MM"), DateUtils.parse(endDate, "yyyy-MM"));
                begin = Integer.valueOf(beginDate.replaceAll("-", ""));
                end = Integer.valueOf(endDate.replaceAll("-", ""));
                for (int i = 0; i <= months; i++) {
                    xAxis.add(DateUtils.format(DateUtils.monthAdd(DateUtils.parseYMD(beginDate + "-01"), i), "yyyy-MM"));
                }
                break;
            default:
                begin = Integer.valueOf(beginDate.replaceAll("-", ""));
                end = Integer.valueOf(endDate.replaceAll("-", ""));
                for (int i = begin; i <= end; i++) {
                    xAxis.add(i + "");
                }
                break;
        }
        vo.setBegin(begin);
        vo.setEnd(end);
        vo.setxAxis(xAxis);
        return vo;
    }
}
